package com.nulldev.util.simplejava;

import java.math.BigDecimal;

import com.nulldev.util.java.JVMUtil;
import com.nulldev.util.java.ReflectionUtils;
import com.nulldev.util.java.var;
import com.nulldev.util.java.swing.SwingUtil;
import com.nulldev.util.logging.Logger;
import com.nulldev.util.logging.LoggerFactory;
import com.nulldev.util.math.lNumber;
import com.nulldev.util.threading.legacy.ThreadExecutor;

import static com.nulldev.util.promise.Promiser.promise;

@SuppressWarnings({"all"})
public class SimpleJava {

	public static <E> var<E> createVar(final E item){
		return new var<E>((E) item);
	}
	
	public static BigDecimal toBigDecimal(final Number num) {
		return new BigDecimal(String.valueOf(num));
	}
	
	public static lNumber toLongNumber(final Number num) {
		return lNumber.overrideBuffer(String.valueOf(num));
	}
	
	public static void println(final Object obj) {
		synchronized (System.out) {
			synchronized (System.err) {
				System.out.println(obj);
			}
		}
	}
	
	public static void debug(final String obj) {
		getLogger().debug(obj);
	}
	
	public static void debugC(final String obj) {
		getLogger().debugC(obj);
	}
	
	public static void errln(final Object obj) {
		synchronized (System.out) {
			synchronized (System.err) {
				System.err.println(obj);
			}
		}
	}
	
	public static void errln(final Throwable error) {
		synchronized (System.out) {
			synchronized (System.err) {
				System.err.println(JVMUtil.fromError(error));
			}
		}
	}
	
	public static void coutln(final String input, final boolean print) {
		if (log_cached == null) {
			return;
		}
		if (print && log_cached.debugEnabled) {
			log_cached.debug(input);
		}
	}
	
	private static SwingUtil swing_cached;
	public static SwingUtil getSwingUtil() {
		if (swing_cached == null) {
			swing_cached = (SwingUtil) ReflectionUtils.createInstance("com.nulldev.util.java.swing.SwingUtil");
		}
		return swing_cached;
	}
	
	private static Logger log_cached;
	public static Logger getLogger() {
		if (log_cached == null) {
			log_cached = LoggerFactory.getLoggerName(
					Thread.currentThread().getStackTrace()[2].getClassName(), true, true);
		}
		return log_cached;
	}
	
	private static ReflectionUtils ref_cached;
	public static ReflectionUtils getReflectionUtils() {
		if (ref_cached == null) {
			ref_cached = new ReflectionUtils();
		}
		return ref_cached;
	}
	
	/**
	 * Runs a runnable task on the ThreadExecutor main thread array.
	 * @param r
	 */
	public static void execute(final Runnable r) {
		ThreadExecutor.execute(r);
	}
	
	public static void exit() {
		exit(-1);
	}
	
	public static void exit(final int status) {
		System.exit(status);
	}
}
